package com.example.zwh.computershop.my_shopp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.Address;

import java.util.ArrayList;

public class AddressActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView addressBack1, addressBack2;
    Button btn_add_address, modify_address;
    TextView add_new_address;
    Intent intent;

    private Myapp myapp;

    //地址参数
    private TextView addree_man_name;
    private TextView address_telephone;
    private TextView address_real;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_address);
        ActivityCollector.addActivity(this);
        initID();
        clickView();
        initData();
    }

    void initID() {
        addressBack1 = findViewById(R.id.address_back);
        addressBack2 = findViewById(R.id.address_back2);
        add_new_address = findViewById(R.id.add_new_address);
        btn_add_address = findViewById(R.id.add_address);
        modify_address = findViewById(R.id.modify_address);

        addree_man_name = findViewById(R.id.addree_man_name);
        address_telephone = findViewById(R.id.address_telephone);
        address_real = findViewById(R.id.address_real);

        myapp = (Myapp) getApplication();
    }

    void initData() {

        //获取地址数据
        Thread t = new Thread(new takeThread());
        t.start();
        try {
            t.join();
        }catch (InterruptedException ie) {
            ie.printStackTrace();
        }

        if (myapp.getAddresses().size() != 0) {
            addree_man_name.setText(myapp.getAddresses().get(0).getName());
            address_telephone.setText(myapp.getAddresses().get(0).getPhone());
            address_real.setText(myapp.getAddresses().get(0).getAddress());
        }
    }

    void clickView() {
        addressBack1.setOnClickListener(this);
        addressBack2.setOnClickListener(this);
        add_new_address.setOnClickListener(this);
        btn_add_address.setOnClickListener(this);
        modify_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.address_back :
                finish();
                break;
            case R.id.address_back2 :
                finish();
                break;
            case R.id.add_address :
                Toast.makeText(this, "添加地址", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, AddModifyAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.add_new_address :
                Toast.makeText(this, "添加新地址", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, AddModifyAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.modify_address :
                Toast.makeText(this, "修改地址", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, AddModifyAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    class takeThread extends Thread {
        @Override
        public void run() {
            if (myapp.getLoState() == 1) {
                ArrayList<Address> adra = new ArrayList<>();
                int state = handleData.getAddre(adra, myapp.getUser().getId());
                if (state == 3) {
                    myapp.setAddresses(adra);
                }
            }
        }
    }
}
